package com.home.job.zh2.job2;


import java.util.*;

public class ShoppingCart {

    public static List<Item> items = new ArrayList<>();
    public static List<Order> orders = new ArrayList<>();

    public static void initItem(){
        // 初始化 商品数量
        Item tissue = new Item("tissue",3, 2, 2);
        Item pen = new Item("pen",5, 3, 2);
        Item snacks = new Item("snacks",2);
        items.add(tissue);
        items.add(pen);
        items.add(snacks);
    }

    public static void addOrder(Item item, int num){
        int existsNumber = 0;
        if (orders.size() > 0) {
            for (Order o: orders) {
                if (null == o.paymentTime && o.goodsId.equals(item.id)){
                    existsNumber = o.goodsNumber;
                    orders.removeIf(order -> item.id.equals(order.goodsId));
                }
            }
        }
        orders.add(new Order(item, (num + existsNumber)));
        System.out.println( item.name + " 成功加入购物车");
    }

    public static void delete(Order order){
        if (orders.size() > 0) {
            orders.removeIf(o -> order.id.equals(o.id));
            System.out.println(order.goodsName + " 订单删除成功");
        } else {
            System.out.println(order.goodsName + "订单不存在");
        }
    }

    public static void search(Item item){
        if (orders.size() > 0) {
            orders.forEach(e -> {
                if (item.id.equals(e.goodsId)) {
                    System.out.println(e);
                }
            });
        } else {
            System.out.println(item.name + "订单不存在");
        }
    }
    public static void pay(Order item){
        if (orders.size() > 0) {
            orders.forEach(e -> {
                if (item.id.equals(e.id)) {
                    if (null == e.paymentTime) {
                        System.out.println("当前订单总价为: " + item.goodsPrice);
                        System.out.println("订单正在支付...");
                        e.paymentTime = new Date();
                        System.out.println(item.goodsName + " 订单支付成功");
                    } else {
                        System.out.println(item.goodsName + " 订单已支付");
                    }
                }
            });
        } else {
            System.out.println(item.goodsName + " 订单不存在");
        }
    }
    public static void pay(){
        if (orders.size() > 0) {
            double price = orders.parallelStream().filter(e -> null == e.paymentTime)
                    .mapToDouble(Order::getGoodsPrice).sum();
            System.out.println("清空购物车总价为: " + price);
            System.out.println("正在清空购物车...");
            orders.forEach(e -> {
                if (null == e.paymentTime){
                    e.paymentTime = new Date();
                }
            });
            System.out.println("购物车清空成功");
        } else {
            System.out.println("购物车为空");
        }
    }
    public static void printItems(){
        for (int i = 0; i < items.size(); i++) {
            System.out.println(i + 1 + ". " + items.get(i).toString());
        }
    }
    public static void printOrders(){
        for (int i = 0; i < orders.size(); i++) {
            System.out.println(i + 1 + ". " + orders.get(i).toString());
        }
    }

    public static int getInt() {
        try {
            Scanner sc = new Scanner(System.in);
            return sc.nextInt();
        }catch(Exception e){
            System.out.println("请输入正确数字序号！");
            return 0;
        }
    }
    public static void getMenu(){
        System.out.println();
        System.out.println("1. 列出所有商品");
        System.out.println("2. 搜索订单");
        System.out.println("3. 添加商品到购物车");
        System.out.println("4. 列出购物车中的所有订单");
        System.out.println("5. 支付订单");
        System.out.println("6. 清空购物车");
        System.out.println("0. 退出");
    }

    public static void main(String[] args) {
        initItem();
        while (true){
            getMenu();
            int menu = getInt();
            if (menu == 0) {
                break;
            }
            switch (menu) {
                case 1 -> printItems();
                case 2 -> {
                    printItems();
                    System.out.println("请输入要搜索的序号: ");
                    int index = getInt() - 1;
                    if (index < 0 || index >= items.size()){
                        System.out.println("请输入正确的数字");
                    } else {
                        search(items.get(index));
                    }
                }
                case 3 -> {
                    printItems();
                    System.out.print("请输入序号选择选择物品添加到购物车: ");
                    int n = getInt() - 1;
                    System.out.print("请输入数量: ");
                    int num = getInt();
                    if (n < 0 || n >= items.size() || num < 0) {
                        System.out.println("请输入正确的数字");
                    } else {
                        addOrder(items.get(n), num);
                        System.out.println("当前购物车:");
                        if (orders.size() > 0) {
                            orders.forEach(System.out::println);
                        }
                    }
                }
                case 4 -> {
                    System.out.println("当前购物车");
                    if (orders.size() > 0) {
                        orders.forEach(System.out::println);
                    }
                    System.out.println("1. 删除订单");
                    System.out.println("输入其他数字返回主菜单");
                    int ss = getInt();
                    if (ss == 1) {
                        printOrders();
                        System.out.println("请输入要删除的序号: ");
                        int index = getInt() - 1;
                        if (index < 0 || index >= orders.size()){
                            System.out.println("请输入正确的数字");
                        } else {
                            delete(orders.get(index));
                        }
                    }
                }
                case 5 -> {
                    printOrders();
                    System.out.println("请输入订单序号:");
                    int index = getInt() - 1;
                    if (index < 0 || index >= orders.size()) {
                        System.out.println("请输入正确的数字");
                    } else {
                        pay(orders.get(index));
                    }
                }
                case 6 -> pay();
                default -> System.out.println("请选择正确的菜单");
            }
        }
    }
}
